<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryForm" :inline="true">
        <el-form-item label="银行卡类型" prop="cardType">
          <el-select v-model="queryForm.cardType" placeholder="请选择" clearable>
            <el-option v-for="item in wageTypes" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="进出场状态" prop="status">
          <el-select v-model="queryForm.status" clearable placeholder="请选择">
            <el-option label="进场" :value="1" />
            <el-option label="离场" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item prop="keywords">
          <el-input v-model="queryForm.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-button v-if="permissions.create" type="primary" size="default" @click="Create">新增</el-button>
          <el-button v-if="permissions.batchImport" size="default" @click="handleBatchImport">批量导入</el-button>
          <el-button v-if="permissions.batchImport" size="default" @click="handleCheckedExport"
            >下载选中人员模板</el-button
          >
        </el-col>
        <el-col :span="24" :offset="0" style="margin-top: 10px">
          <el-table
            ref="tableRef"
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            :show-overflow-tooltip="{ effect: 'light' }"
          >
            <el-table-column type="selection" width="50" />
            <el-table-column label="序号" type="index" :index="(index: number) => index + 1" min-width="50" />
            <el-table-column label="姓名" prop="personName" min-width="100" />
            <el-table-column label="证件号码" prop="idCard" min-width="120" />
            <!-- <el-table-column label="参建单位" prop="corpName" min-width="100" />
            <el-table-column label="人员类型" prop="personType" min-width="100" /> -->
            <!-- <el-table-column
              label="是否默认"
              prop="isDefault"
              :formatter="(item) => (item.isDefault ? '是' : '否')"
              min-width="80"
            /> -->
            <el-table-column label="工资卡类别" prop="typeName" min-width="80" />
            <el-table-column label="工资卡卡号" prop="cardNo" min-width="120" />
            <el-table-column label="工资卡开卡银行" prop="bankName" min-width="120" />
            <el-table-column label="创建时间" prop="createTime" min-width="120" />
            <el-table-column
              v-if="permissions.update || permissions.delete"
              label="操作"
              fixed="right"
              max-width="260"
              min-width="120"
            >
              <template #default="scope">
                <el-button v-if="permissions.update" link size="small" type="primary" @click="Edit(scope.row)">
                  修改
                </el-button>
                <el-button v-if="permissions.delete" type="primary" link size="small" @click="Delete(scope.row)">
                  删除
                </el-button>
                <!-- <el-button v-if="permissions.detail" type="primary" link size="small" @click="Detail(scope.row)">
                  详情
                </el-button> -->
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryForm.pageNum"
            v-model:limit="queryForm.pageSize"
            @pagination="handleQuery"
          />
        </el-col>
      </el-row>
    </div>
    <add-form :dialog-add-or-edit="dialogAddOrEdit" @send-close="sendClose" />
    <import-excel
      v-if="dialogImportExcel.visible"
      :dialog-import-excel="dialogImportExcel"
      @send-close="sendClose"
    ></import-excel>
    <!-- <detail :dialog-detail="dialogDetail" @send-close="sendClose" /> -->
  </div>
</template>
<script setup lang="ts">
import addForm from "./addForm.vue";
import importExcel from "./importExcel.vue";
import { getPersonBankList, deletePersonBank, exportCheckedBankTemp } from "@/api/wage/index";
import { getDicts } from "@/store/modules/dict";
import router from "@/router";

import { downloadFile } from "@/utils";

const permissions = ref({
  create: true,
  update: true,
  delete: true,
  detail: true,
  batchImport: true,
});
const wageTypes = getDicts("personCardType");
const constructionStatus = getDicts("constructionStatus");
const currentSystem = ref(localStorage.getItem("usertypeName"));
const isLumpContractor = ref(localStorage.getItem("isLumpContractor"));

if (currentSystem.value == "Constructor" || currentSystem.value == "Commander" || currentSystem.value == "SysUser") {
  permissions.value = {
    create: false,
    update: false,
    delete: false,
    detail: false,
    batchImport: false,
  };
}

console.log(isLumpContractor);
console.log(currentSystem);

const loading = ref(false);
const tableData = ref();
const dialogAddOrEdit = reactive({
  corpId: undefined,
  id: undefined,
  title: "",
  footBtn: "",
  visible: false,
});

const dialogImportExcel = reactive<DialogOption>({
  visible: false,
});

const dialogDetail = reactive<DialogOption>({
  visible: false,
});
const currentProject = ref();
const tableRef = ref();

//新增
const Create = function () {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "确认新增";
  dialogAddOrEdit.title = "新增";
};
//编辑
const Edit = function (row: any) {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.id = row.id;
  dialogAddOrEdit.corpId = row.corpId;
  dialogAddOrEdit.footBtn = "确认修改";
  dialogAddOrEdit.title = "修改";
};

//详情
const Detail = function (row: any) {
  dialogDetail.visible = true;
  dialogDetail.id = row.id;
  dialogDetail.title = "详情";
};

const sendClose = function (needRefresh: boolean) {
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  dialogDetail.visible = false;
  dialogDetail.id = undefined;
  dialogImportExcel.visible = false;
  if (needRefresh) {
    handleQuery();
  }
};

const handleBatchImport = () => {
  dialogImportExcel.visible = true;
  dialogImportExcel.visible = true;
  dialogImportExcel.title = "批量导入工资卡";
};

//删除
const Delete = function (row: any) {
  ElMessageBox.confirm("确定删除吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deletePersonBank(row.id).then((res) => {
        if (res.type === 0) {
          if (res.success) {
            ElMessage({
              type: "success",
              message: res.msg,
            });
            handleQuery();
          } else {
            ElMessage({
              type: "error",
              message: res.msg,
            });
          }
        }
      });
    })
    .catch(() => {
      console.log("取消");
    });
};

//查询条件
const queryForm = reactive({
  cardType: undefined,
  keywords: "",
  pageNum: 1,
  pageSize: 10,
});
const total = ref(0);

//这是列表数据
const handleQuery = function () {
  loading.value = true;
  getPersonBankList(queryForm)
    .then((res) => {
      if (res.type === 0) {
        if (res.success) {
          tableData.value = res.data.list;
          queryForm.pageNum = res.data.pageNum;
          queryForm.pageSize = res.data.pageSize;
          total.value = res.data.total;
        } else {
          ElMessage.warning(res.msg);
        }
      } else {
        ElMessage.warning(res.msg);
      }
    })
    .finally(() => {
      loading.value = false;
    });
};
const handleCheckedExport = () => {
  const checkedRows = tableRef.value!.getSelectionRows();
  if (checkedRows.length < 1) {
    ElMessage.warning("请选择要导出的人员");
    return false;
  }
  const reqData = checkedRows.map((item: any) => {
    return { name: item.personName, idCard: item.idCard };
  });
  exportCheckedBankTemp(reqData).then((res) => {
    downloadFile(res);
  });
};

onMounted(() => {
  handleQuery();
});
</script>
<style scoped>
:deep(.el-button) {
  margin-left: 5px;
}
:deep(.el-input__wrapper) {
  border-radius: 0;
}
</style>
